import { Controller,Post,Body,Get,Delete,Request,Param } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose'
import { IsNotEmpty } from 'class-validator'
import { ApiBasicAuth,ApiOkResponse,ApiCreatedResponse, ApiTags, ApiOperation, ApiProperty  } from '@nestjs/swagger'
import { phones,emails } from '../utils'
class shopID{
  @ApiProperty({ description:'',example:'id(required)' })
  id:string
}
class shopCatDto{
  @ApiProperty({ description:'',example:'id' })
  id:string
  @ApiProperty({ description:'',example:'商户名(required)' })
  // 使用管道进行
  @IsNotEmpty({message:'用户名必填'})
  username: string
  @ApiProperty({ description:'',example:'手机号(required)'})
  @IsNotEmpty({message:'手机号必填'})
  phone: string
  @ApiProperty({ description:'',example:'邮箱(required)'})
  @IsNotEmpty({message:'邮箱必填'})
  mailbox: string
  @ApiProperty({ description:'',example:'商品名称(required)'})
  @IsNotEmpty({message:'商品名称必填'})
  shopName: string
  @ApiProperty({ description:'',example:'商品描述'})
  textHtml: string
}
class shopResponses{
  @ApiProperty({ description:'',example:'id' })
  id:string
  @ApiProperty({ description:'',example:'商户名' })
  username: string
  @ApiProperty({ description:'',example:'手机号'})
  phone: string
  @ApiProperty({ description:'',example:'邮箱'})
  mailbox: string
  @ApiProperty({ description:'',example:'商品名称'})
  shopName: string
  @ApiProperty({ description:'',example:'商品描述'})
  textHtml: string
  @ApiProperty({ description:'',example:'操作人'})
  anyname: string
  @ApiProperty({ description:'',example:'创建时间'})
  startTime:number
  @ApiProperty({ description:'',example:'修改时间'})
  lastTime:number
}
class shopQuery{
  @ApiProperty({ description:'',example:'搜索(根据用户名搜索)' })
  query: string
  @ApiProperty({ description:'',example:'页数' })
  pagenum:number
  @ApiProperty({ description:'',example:'条数' })
  pagesize:number
  @ApiProperty({ description:'',example:'总条数' })
  total:number
}
@ApiTags('信息总览')
@Controller('shop')
export class ShopController {
  constructor(
    @InjectModel('Cat') private readonly studentsModel,
    @InjectModel('shopName') private readonly shopServices
  ){}
@Post('anyShop')
// 接口响应文档示例
@ApiCreatedResponse({description: '', type: shopResponses})
@ApiOperation({ summary: '获取数据列表' })
  async anyShop(@Body() body:shopQuery){
    // 如果不传查询条件则默认请求10条数据
    body.pagenum = body.pagenum ? body.pagenum : 1
    body.pagesize = body.pagesize ? body.pagesize : 10
    var dataList = await this.shopServices.find().sort({_id: -1})
    // 接口支持模糊搜索
    if (body.query!=='') {      
       dataList = dataList.filter(x=>{
        if(x.username.indexOf(body.query)!==-1){
          return x
        }
      })  
    }
    // 分页
    var dataListNumber = body.pagenum*body.pagesize > dataList.length ? dataList.length : body.pagesize
    const dataLists=dataList.slice((body.pagenum-1)*body.pagesize, dataListNumber )
    
    return {status:'200',data:dataLists,total:dataList.length,message:'查询成功'}
  }

// 添加接口
@Post('updates')
@ApiOperation({ summary: '用户添加' })
async updateShop(@Body() body:shopCatDto,@Request() request){  
  if (!phones(body.phone)) return {status:'200',data:[],message:'请输入正确手机号'}
  if (!emails(body.mailbox)) return {status:'200',data:[],message:'请输入正确邮箱地址'}
  const result = await this.studentsModel.find({tokens:request.headers.authorization})
  if (result[0].userSession==='0') return {status:'501',data:[],message:'权限等级不足'}
  const lists = {
    username:body.username,
    phone:body.phone,
    mailbox:body.mailbox,
    shopName:body.shopName,
    textHtml:body.textHtml,
    anyname:result[0].username,
    startTime:Date.now(),
    lastTime:Date.now()
  }
  await this.shopServices.create(lists)
  return {status:'200',data:[],message:'添加成功'}
  }

// 查询接口
@Get(':id')
@ApiOperation({ summary: '数据详情' })
async findShop(@Param() param){
  const lists = await this.shopServices.findById({_id:param.id})
  return {status:'200',data:lists,message:'查询成功'}
}
// 更新接口
@Post('shopSaves')
@ApiOperation({ summary: '更新接口' })
async shopSaves(@Body() body:shopCatDto,@Request() request){
  if (!body.id) return {status:'200',data:[],message:'id不能为空'}
  if (!phones(body.phone)) return {status:'200',data:[],message:'请输入正确手机号'}
  if (!emails(body.mailbox)) return {status:'200',data:[],message:'请输入正确邮箱地址'}
  const result = await this.studentsModel.find({tokens:request.headers.authorization})
  if (result[0].userSession==='0') return {status:'501',data:[],message:'权限等级不足'}
  const dataList = await this.shopServices.findById({_id:body.id})
  // 由于moogodb数据库特殊性  对于非必填字段建议用三元表达式处理下
  // mysql没有这个问题
  dataList.username = body.username
  dataList.phone = body.phone
  dataList.mailbox = body.mailbox
  dataList.shopName = body.shopName
  dataList.textHtml = body.textHtml ? body.textHtml : dataList.textHtml
  
  dataList.anyname=result[0].username,
  dataList.lastTime = Date.now()
  await dataList.save()
  return {status:'200',data:[],message:'更新成功'}
  }

  // 删除接口
@Delete(':id')
@ApiOperation({ summary: '删除接口' })
async shopRemove(@Param() param:shopID,@Request() request){  
  if (!param.id) return {status:'200',data:[],message:'id不能为空'}
  const result = await this.studentsModel.find({tokens:request.headers.authorization})
  if (result[0].userSession==='0') return {status:'501',data:[],message:'权限等级不足'}
  const dataList = await this.shopServices.findById({_id:param.id})
  await dataList.remove()
  return {status:'200',data:[],message:'删除成功'}
  }
}
